package gui.win.genotype;

import core.utils.Utils;
import gui.Segl1;
import gui.processor.Processor;
import gui.processor.Worker;
import gui.utils.Para;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Buttons extends JPanel implements ActionListener{
    private JButton close, create;
    private Genotype genotype;
    private Segl1 segL1;
    public Buttons(Genotype genotype, Segl1 segL1){
        super(new BorderLayout());
        this.setBorder(BorderFactory.createEmptyBorder(0,6,6,6));
        this.genotype = genotype;
        this.segL1 = segL1;
        this.add(Utils.getButton(close = new JButton("CANCEL"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Cancel"), BorderLayout.WEST);
        this.add(Utils.getButton(create = new JButton("GENOTYPE"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Genotype"), BorderLayout.EAST);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == close){genotype.dispose();}
        else if(source == create){

            boolean atLeastOneCodon = false;
            if(Para.GENOCODS!=null){
                for(int x=0; x<Para.GENOCODS.length; x++){if(Para.GENOCODS[x]){atLeastOneCodon=true;x=Para.GENOCODS.length+1;}}
            }
            boolean atLeastOneAA = false;
            for(int x=0; x<Para.GENOAA.length; x++){if(Para.GENOAA[x]){atLeastOneAA=true;x=Para.GENOAA.length+1;}}
            if(Para.GENOQUERY==null){JOptionPane.showMessageDialog(null, "No DNA was pasted in to the location search.", "alert", JOptionPane.ERROR_MESSAGE);}
            else if(!atLeastOneCodon){JOptionPane.showMessageDialog(null, "No codons were selected for testing.", "alert", JOptionPane.ERROR_MESSAGE);}
            else if(!atLeastOneAA){JOptionPane.showMessageDialog(null, "No amino acids were selected to test for.", "alert", JOptionPane.ERROR_MESSAGE);}
            else{
                Processor guip = new Processor(segL1);
                guip.setTask(Para.TASK_GENOTYPE);
                Worker sl = new Worker(guip);
                sl.execute();
                genotype.dispose();
            }
        }
    }
    public void closeFrame(){genotype.closeFrame();}
}